package com.government.web.controller.article;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.government.article.bo.ArticlePolicyBO;
import com.government.article.bo.NormalPolicyBO;
import com.government.article.domain.Article;
import com.government.article.vo.AllPolicyVO;
import com.government.article.vo.ArticlePolicyVO;
import com.government.common.annotation.RepeatSubmit;
import com.government.common.config.GovernmentConfig;
import com.government.common.constant.Constants;
import com.government.common.core.domain.BaseTreeVo;
import com.government.common.utils.StringUtils;
import com.government.framework.config.ServerConfig;
import com.government.member.domain.UserPlan;
import com.government.util.Documents4jUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.government.common.annotation.Log;
import com.government.common.core.controller.BaseController;
import com.government.common.core.domain.AjaxResult;
import com.government.common.enums.BusinessType;
import com.government.article.service.IArticlePolicyService;
import com.government.common.utils.poi.ExcelUtil;
import com.government.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 政策分类信息Controller
 *
 * @author system
 * @date 2023-09-25
 */
@RestController
@RequestMapping("/article/article_policy")
public class ArticlePolicyController extends BaseController {
    @Autowired
    private IArticlePolicyService articlePolicyService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询政策分类信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ArticlePolicyBO bo) {
        startPage();
        List<ArticlePolicyVO> list = articlePolicyService.selectArticlePolicyList(bo);
        return getDataTable(list);
    }

    @GetMapping("/selectAllPolicy")
    public AjaxResult selectAllPolicy() {
        List<AllPolicyVO> list = articlePolicyService.selectAllPolicy();
        return success(list);
    }

    @GetMapping("/selectPolicyTree")
    public AjaxResult selectPolicyTree() {
        List<BaseTreeVo> list = articlePolicyService.selectPolicyTree();
        return success(list);
    }

    /**
     * 导出政策分类信息列表
     */
    @RepeatSubmit
    @Log(title = "政策分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArticlePolicyBO bo) {
        List<ArticlePolicyVO> list = articlePolicyService.selectArticlePolicyList(bo);
        ExcelUtil<ArticlePolicyVO> util = new ExcelUtil<ArticlePolicyVO>(ArticlePolicyVO.class);
        util.exportExcel(response, list, "政策分类信息数据");
    }

    /**
     * 获取政策分类信息详细信息
     */
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId) {
        return success(articlePolicyService.selectArticlePolicyByArticleId(articleId));
    }

    /**
     * 新增政策分类信息
     */
    @RepeatSubmit
    @Log(title = "政策分类信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticlePolicyBO bo) {
        return toAjax(articlePolicyService.insertArticlePolicy(bo));
    }

    /**
     * 修改政策分类信息
     */
    @RepeatSubmit
    @Log(title = "政策分类信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArticlePolicyBO bo) {
        return toAjax(articlePolicyService.updateArticlePolicy(bo));
    }

    /**
     * 删除政策分类信息
     */
    @RepeatSubmit
    @Log(title = "政策分类信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleId}")
    public AjaxResult remove(@PathVariable Long[] articleId) {
        articlePolicyService.deleteArticlePolicyByArticleId(articleId);
        return toAjax(true);
    }

    /**
     * 政策匹配
     */
    @GetMapping("/normalPolicy")
    public AjaxResult normalPolicy(NormalPolicyBO bo) {

        return AjaxResult.success(articlePolicyService.normalPolicy(bo));
    }

    /**
     * 根据资讯id查找所属政策树
     */

    @GetMapping("/getRelationPolicy")
    public AjaxResult getRelationPolicy(Long articleId) {
        return AjaxResult.success(articlePolicyService.getRelationPolicy(articleId));
    }

    /**
     * 根据政策id查找所属政策树
     */

    @GetMapping("/getParentPolicy")
    public AjaxResult getParentPolicy(Long policyId) {
        return AjaxResult.success(articlePolicyService.getParentPolicy(policyId));
    }

    /**
     * 根据政策id查找关联政策树
     */

    @GetMapping("/findPolicyRel")
    public AjaxResult findPolicyRel(Long policyId) {
        return AjaxResult.success(articlePolicyService.findPolicyRel(policyId));
    }

    /**
     * 根据资讯id查找管理政策树
     */

    @GetMapping("/findPolicyManageRel")
    public AjaxResult findPolicyManageRel(Long policyId) {
        return AjaxResult.success(articlePolicyService.findPolicyManageRel(policyId));
    }

    /**
     * 根据资讯id查找演变政策树
     */

    @GetMapping("/findPolicyEvolutionRel")
    public AjaxResult findPolicyEvolutionRel(Long policyId) {
        return AjaxResult.success(articlePolicyService.findPolicyEvolutionRel(policyId));
    }

    /**
     * 查找用户规划列表
     */

    @GetMapping("/getUserPlan")
    public AjaxResult getUserPlan() {
        return AjaxResult.success(articlePolicyService.getUserPlan());
    }

    /**
     * 查询规划详情
     */

    @GetMapping("/getUserPlanDetail")
    public AjaxResult getUserPlanDetail(Long id) {
        return AjaxResult.success(articlePolicyService.getUserPlanDetail(id));
    }

    /**
     * 查询网址下拉列表
     */

    @GetMapping("/getWebUrlList")
    public AjaxResult getWebUrlList() {
        return AjaxResult.success(articlePolicyService.getWebUrlList());
    }

    /**
     * 保存用户规划
     */
    @RepeatSubmit
    @PostMapping("/saveUserPlan")
    public AjaxResult saveUserPlan(@RequestBody UserPlan userPlan) {
        Long id = articlePolicyService.saveUserPlan(userPlan);
        return AjaxResult.success(id);
    }

    /**
     * 复制政策分类信息
     */
    @RepeatSubmit
    @PostMapping("/copy")
    public AjaxResult copy(@RequestBody Article article) {
        articlePolicyService.copyArticlePolicyByArticleId(article.getArticleId());
        return AjaxResult.success();
    }

    /**
     * 上传word转pdf
     *
     * @param file
     * @param
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @PostMapping("/wordToPdf")
    public AjaxResult wordToPdf(MultipartFile file) throws Exception {

        Documents4jUtil.convertWordToPdf(file.getInputStream(), file.getOriginalFilename());
        int dirLastIndex = GovernmentConfig.getProfile().length() + 1;
        String currentDir = StringUtils.substring(GovernmentConfig.getUploadPath(), dirLastIndex);
        String url = serverConfig.getUrl()+ Constants.RESOURCE_PREFIX+ "/"+currentDir+ "/convert/"+ file.getOriginalFilename().substring(0, file.getOriginalFilename().indexOf("."))+".pdf";
        return success(url);
    }
}

